package com.example.liaotian.Fragment.UpFragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.liaotian.R;
import com.example.liaotian.view.LooperPager;
import com.example.liaotian.view.PagerItem;

import java.util.ArrayList;
import java.util.List;

/**主页面
 * A simple {@link Fragment} subclass.
 */
public class HomeUpFragment extends Fragment {
    private LooperPager looperPager;
    private List<PagerItem> list = new ArrayList<>();

    public HomeUpFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home_up, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        intiData();//这里数据填充要写在加载组件上面，如果写在下面，需要在方法内部写上刷新数据的方法
        intiView();//加载组件的方法
    }
    private void intiData(){
        list.add(new PagerItem("广告位招租！",R.mipmap.mig1));
        list.add(new PagerItem("广告位招租！",R.mipmap.mig11));
        list.add(new PagerItem("广告位招租！",R.mipmap.mig111));

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void intiView() {
        //绑定布局
        looperPager = getView().findViewById(R.id.loopPager);
        //设置数据
        looperPager.setData(new LooperPager.nInnerAdapter() {
            @Override
            protected int getDataSize() {
                return list.size();
            }

            @Override
            public View getSubView(ViewGroup container, int position) {
                ImageView imageView =  new ImageView(container.getContext());
                imageView.setImageResource(list.get(position).getPicResId());
                return imageView;

            }
        } , new LooperPager.BindTitleLisener() {
            @Override
            //获得当前位置图片的标题
            public String getTitle(int position) {
                return list.get(position % list.size()).getTitle();
            }


        });
    }
}
